<?php
class ControllerAddonBdsdel extends Controller
{
	private $error = array();
	private $sitemapid;
	public function __construct() 
	{
		
		
		$this->load->model("addon/bds");
		$this->load->model("addon/nhom");
		$this->load->model("core/user");
		$this->load->helper('image');
		$this->data['nhucau'] = array();
		$this->model_addon_nhom->getTree("nhucau",$this->data['nhucau']);
		unset($this->data['nhucau'][0]);
		
		$this->data['loaibds'] = array();
		$this->model_addon_nhom->getTree("loaibds",$this->data['loaibds']);
		unset($this->data['loaibds'][0]);
		
		$this->data['huong'] = array();
		$this->model_addon_nhom->getTree("huong",$this->data['huong']);
		unset($this->data['huong'][0]);
		
		$this->data['khuvuc'] = array();
		$this->model_addon_nhom->getTree("khuvuc",$this->data['khuvuc']);
		unset($this->data['khuvuc'][0]);
		
		$where = " AND 	usertypeid <> 'member' ";
		$this->data['users'] = $this->model_core_user->getList($where);
		
	}
	
	public function index()
	{
		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model("addon/bds");
		$this->getList();
	}
	
	
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('addon/bds/insert&sitemapid='.$this->sitemapid);
		$this->data['delete'] = $this->url->http('addon/bds/delete');		
		
		$where = "";
		$datasearchlike['bdsid'] = $this->request->get['bdsid'];
		$datasearchlike['tieude'] = $this->request->get['tieude'];
		$datasearch['nhucau'] = $this->request->get['nhucau'];
		$datasearch['loaibds'] = $this->request->get['loaibds'];
		$datasearch['huong'] = $this->request->get['huong'];
		$datasearch['khuvuc'] = $this->request->get['khuvuc'];
		
		$arr = array();
		foreach($datasearchlike as $key => $item)
		{
			if($item !="")
				$arr[] = " AND " . $key ." like '%".$item."%'";
		}
		
		foreach($datasearch as $key => $item)
		{
			if($item !="")
				$arr[] = " AND " . $key ." = '".$item."'";
		}
		
		if($this->request->get['giatu'])
		{
			$arr[] = " AND gia >= '".$this->string->toNumber($this->request->get['giatu'])."'";
		}
		
		if($this->request->get['giaden'])
		{
			$arr[] = " AND gia <= '".$this->string->toNumber($this->request->get['giaden'])."'";
		}
		
		$where = implode("",$arr);
		
		if($this->user->getUserTypeId()!="admin")
		{
			$where.=" AND assignto = '".$this->user->getId()."'";	
		}
		
		$where.=" ORDER BY `dlbds`.`statusdate` DESC";
		
		$this->data['datas'] = array();
		$rows = $this->model_addon_bds->getListDel($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['imagethumbnail'] = HelperImage::resizePNG($this->data['datas'][$i]['imagepath'], 100, 100);
			
			
			
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="addon/bdsdel_list.tpl";
		$this->layout="layout/center";
		
		$this->render();
	}
	
	public function restore()
	{
		$bdsid = $this->request->get['bdsid'];
		$this->load->model("addon/bds");
		$data = $this->model_addon_bds->getItem($bdsid);
		$data['status'] = "";
		$this->model_addon_bds->updateStatus($data);
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	
}
?>